//
//  ViewController.m
//  DYHospital
//
//  Created by 胡金东 on 16/3/21.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "ViewController.h"
#import "DYSearchViewController.h"
#import "DYLoginViewController.h"
#import "UIView+Color.h"
#import "DYOrderViewController.h"
#import "DYHealthTestViewController.h"
#import "DYHealthViewController.h"

#define IMAGECOUNT 6

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIScrollView *scrollViewBtn;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *timer;

@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem * btn = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = btn;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"大友";
//    [self.view addSubview:self.title];
    
    
    self.scrollViewBtn = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollViewBtn.delegate = self;

    self.automaticallyAdjustsScrollViewInsets = YES;
   self.scrollViewBtn.showsVerticalScrollIndicator = NO;//垂直移动
   self.scrollViewBtn.showsHorizontalScrollIndicator = NO;//水平移动
    //上下滑动
    self.scrollViewBtn.contentSize = CGSizeMake(self.view.frame.size.width,250 + (self.view.frame.size.width-15)/3 * 4 + 10) ;


    [self.view addSubview:self.scrollViewBtn];

    [self addSearchBtn];
    [self setupScrollView];
    [self setupPageControl];
    [self setUpButton];
    [self signUpBtn];
    
}



-(void)signUpBtn
{
    UIButton * signUpBtn = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 45, 45)];
    signUpBtn.backgroundColor = [UIColor lightGrayColor];
    [signUpBtn setTitle:@"登录" forState:UIControlStateNormal];
    signUpBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [signUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [signUpBtn addTarget:self action:@selector(signUpBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [signUpBtn setRadius:8 borderWith:0 borderColor:[UIColor clearColor]];

    [self.scrollViewBtn insertSubview:signUpBtn aboveSubview:self.scrollView];
}
-(void)signUpBtnClick
{
    DYLoginViewController *login = [[DYLoginViewController alloc]init];
    [self.navigationController pushViewController:login animated:YES];
}

//添加搜索按钮
-(void)addSearchBtn
{
    
    UIBarButtonItem *searchBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Search"] style:UIBarButtonItemStyleDone target:self action:@selector(searchBtnClick)];
    [searchBtnItem setTintColor:[UIColor blackColor]];
    self.navigationItem.rightBarButtonItem = searchBtnItem;
}

-(void)searchBtnClick
{
    DYSearchViewController * dySearch = [[DYSearchViewController alloc]init];
    
    [self presentModalViewController:dySearch animated:YES];
}
//首页轮播
-(void)setupScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 166)];
     self.scrollView.delegate = self;
    for (int i = 0; i<IMAGECOUNT; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, 166)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]];
        imageView.userInteractionEnabled = YES; // UIImageView默认不允许用户交互
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)];
        [imageView addGestureRecognizer:singleTap];
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(IMAGECOUNT * self.view.frame.size.width, 166) ;
    self.automaticallyAdjustsScrollViewInsets = NO; //ScrollView不能在viewController划到顶
    self.scrollView.pagingEnabled = YES;//分页设置
    self.scrollView.showsVerticalScrollIndicator = NO;//垂直移动
    self.scrollView.showsHorizontalScrollIndicator = NO;//水平移动
    self.scrollView.bounces = NO;//NO时滚动不会超出可见范围
    [self.scrollViewBtn addSubview:self.scrollView];
    [self addTimer];
}

-(void)onClickImage
{
    NSLog(@"点击图片");
}

-(void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    self.timer = timer;
    //循环
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
    
}

-(void)nextImage
{
    NSInteger page = self.pageControl.currentPage;
    if (page == self.pageControl.numberOfPages-1) {
        page = 0;
    }else{
        page ++;
    }
    CGFloat offsetX = page * self.scrollView.frame.size.width;
    [UIView animateWithDuration:1.0 animations:^{
        self.scrollView.contentOffset = CGPointMake(offsetX, 0);
    }];

}
-(void)setupPageControl
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width-100)/2+20,210,40,20)];
    self.pageControl.numberOfPages = IMAGECOUNT;
    self.pageControl.enabled = NO;
    self.pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    [self.scrollViewBtn insertSubview:self.pageControl aboveSubview:self.scrollView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint page = self.scrollView.contentOffset;
    self.pageControl.currentPage = page.x /[UIScreen mainScreen].bounds.size.width + 0.5;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    //将要拖拽是停止定时器
    [self.timer invalidate];
}
//结束拖拽调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

-(void)setUpButton
{
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 0;
    CGFloat h = 0;
    
    x = 5;
    y = 250;
    w = (self.view.frame.size.width-15)/3;
    h = (self.view.frame.size.width-15)/3 *2;
    UIButton *OrderBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [OrderBtn setTitle:@"预约挂号" forState:UIControlStateNormal];
    [OrderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    OrderBtn.backgroundColor = [UIColor greenColor];
    [OrderBtn addTarget:self action:@selector(OrderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:OrderBtn];
    
    x = w + x * 2;
    y = y;
    w = h;
    h = h/2;
    UIButton *duqueBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [duqueBtn setTitle:@"排队候诊" forState:UIControlStateNormal];
    [duqueBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    duqueBtn.backgroundColor = [UIColor purpleColor];
    [duqueBtn addTarget:self action:@selector(duqueBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:duqueBtn];
    
    x = x ;
    y = y + (self.view.frame.size.width-15)/3 + 5;
    w = (self.view.frame.size.width-15)/3 - 5;
    h = (self.view.frame.size.width-15)/3 - 5;
    UIButton *HospBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [HospBtn setTitle:@"医院导航" forState:UIControlStateNormal];
    [HospBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    HospBtn.backgroundColor = [UIColor redColor];
    [HospBtn addTarget:self action:@selector(HospBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:HospBtn];

    x = (self.view.frame.size.width-15)/3 * 2 + 10;
    y = y;
    w = (self.view.frame.size.width-15)/3;
    h = (self.view.frame.size.width-15)/3 * 2;
    
    UIButton *schedBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [schedBtn setTitle:@"门诊排班" forState:UIControlStateNormal];
    [schedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    schedBtn.backgroundColor = [UIColor orangeColor];
    [schedBtn addTarget:self action:@selector(schedBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:schedBtn];
    
    x = 5;
    y = y + w ;
    w = h;
    h = (self.view.frame.size.width-15)/3;
    UIButton *healthTestBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [healthTestBtn setTitle:@"健康自测" forState:UIControlStateNormal];
    [healthTestBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    healthTestBtn.backgroundColor = [UIColor cyanColor];
    [healthTestBtn addTarget:self action:@selector(healthTestBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:healthTestBtn];
    
    x = 5;
    y = y + h + 5;
    w = h;
    h = h;
    UIButton *healthBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [healthBtn setTitle:@"健康咨询" forState:UIControlStateNormal];
    [healthBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    healthBtn.backgroundColor = [UIColor magentaColor];
    [healthBtn addTarget:self action:@selector(healthBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:healthBtn];
    
    x = h + 2 * x;
    y = y;
    w = h * 2;
    h = h;
    UIButton *informBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [informBtn setTitle:@"取报告单" forState:UIControlStateNormal];
    [informBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    informBtn.backgroundColor = [UIColor blueColor];
    [informBtn addTarget:self action:@selector(informBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollViewBtn addSubview:informBtn];

    
    
}
//点击预约
-(void)OrderBtnClick
{
    DYOrderViewController *orderVC = [[DYOrderViewController alloc]init];
    [self.navigationController pushViewController:orderVC animated:YES];
}

-(void)duqueBtnClick
{
    NSLog(@"点击排队");
}

-(void)HospBtnClick
{
    NSLog(@"点击医院");
}


-(void)schedBtnClick
{
    NSLog(@"点击排班");
}

-(void)healthTestBtnClick
{
    DYHealthTestViewController * healthTestVC = [[DYHealthTestViewController alloc]init];
    [self.navigationController pushViewController:healthTestVC animated:YES];
//    NSLog(@"点击健康测试");
}

-(void)healthBtnClick
{
    DYHealthViewController * healthVC = [[DYHealthViewController alloc]init];
    [self.navigationController pushViewController:healthVC animated:YES];
    
//    NSLog(@"点击健康咨询");
}


-(void)informBtnClick
{
    NSLog(@"点击报告单");
}
















@end
